package swp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swp.dao.ClientDAO;
import swp.vo.Client;

import com.sun.org.apache.xml.internal.serializer.ElemDesc;

/**
 * Servlet implementation class clientservlet
 */
@WebServlet("/clientservlet")
public class ClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClientServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		
		if(method == null){
			return;
		}
		else if(method.equals("clientlist")){
			clientList(request, response);
		}
		else if(method.equals("clientadd")){
			clientAdd(request, response);
		}
		else if(method.equals("client")){
			client(request, response);
		}
		else if(method.equals("clientdelete")){
			clientDelete(request, response);
		}
		else if(method.equals("clientupdate")){
			clientUpdate(request, response);
		}
		else if(method.equals("clientquerylist")){
			clientQueryList(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub			
		doGet(request, response);
	}
	
	private void client(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		String clientId = request.getParameter("clientId");
		
		try {
			request.setAttribute("Client", ClientDAO.getClient(clientId));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientUpdate.jsp";
		forward(request, response, forward);
	}
	
	private void clientList(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		
		try {
			request.setAttribute("Clients", ClientDAO.getClientList());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientList.jsp";
		forward(request, response, forward);
	}
	
	private void clientQueryList(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		String queryCondition = request.getParameter("queryCondition");
		String queryConditionVlue = request.getParameter("queryConditionVlue");
		
		try {
			request.setAttribute("Clients", ClientDAO.getClientQueryList(queryCondition, queryConditionVlue));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientList.jsp";
		forward(request, response, forward);
	}
	
	private void clientAdd(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		Client client = new Client();
		String clientName = request.getParameter("clientName");
		String clientPhoneNumber = request.getParameter("clientPhoneNumber");
		String clientAddress = request.getParameter("clientAddress");
		String clientIdCardNumber = request.getParameter("clientIdCardNumber");
		String isPay = request.getParameter("isPay");
		
		try {
			client.setClientName(clientName);
			client.setClientPhoneNumber(clientPhoneNumber);
			client.setClientAddress(clientAddress);
			client.setClientIdCardNumber(clientIdCardNumber);
			client.setIsPay(isPay);
			
			ClientDAO.addClient(client);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientservlet?method=clientlist";
		forward(request, response, forward);
	}
	
	private void clientUpdate(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		Client client = new Client();
		String clientId = request.getParameter("clientId");
		String clientName = request.getParameter("clientName");
		String clientPhoneNumber = request.getParameter("clientPhoneNumber");
		String clientAddress = request.getParameter("clientAddress");
		String clientIdCardNumber = request.getParameter("clientIdCardNumber");
		String isPay = request.getParameter("isPay");
		
		try {
			client.setClientId(clientId);
			client.setClientName(clientName);
			client.setClientPhoneNumber(clientPhoneNumber);
			client.setClientAddress(clientAddress);
			client.setClientIdCardNumber(clientIdCardNumber);
			client.setIsPay(isPay);
			
			ClientDAO.updateClient(client);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientservlet?method=clientlist";
		forward(request, response, forward);
	}
	
	private void clientDelete(HttpServletRequest request, HttpServletResponse response) {
		String forward = null;
		String clientId = request.getParameter("clientId");
		
		try {
			ClientDAO.deleteClient(clientId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		forward = "clientservlet?method=clientlist";
		forward(request, response, forward);
	}
	

	
	private void forward(HttpServletRequest request, HttpServletResponse response, String url) {
		try {
			request.getRequestDispatcher(response.encodeURL(url)).forward(
					request, response);
		} catch (Exception es) {
			es.printStackTrace();
}
}

}
